﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyFeer.Widget.Grid.Cell
{
    public class CellItem
    {
        private Rect rect;
        private CellModel cellModel;
        private GridViewer gridViewer;
        private TextBlock textBlock;
        public CellItem(GridViewer gridViewer,CellModel cellModel)
        {
            this.gridViewer = gridViewer;
            this.cellModel = cellModel;
        }
        public void SetEditorString(String editorString)
        {
            textBlock.Text = editorString;
        }
        internal void render(int x, int y,int width,int height)
        {
            Canvas mainCanvas=this.gridViewer.GridCanvas;
            Border border = new Border()
            {
                Width=width,
                Height=height
            };
            Canvas.SetLeft(border, x);
            Canvas.SetTop(border, y);
            mainCanvas.Children.Add(border);

            if (cellModel != null)
            {
                if (cellModel.CellStyle != null)
                {
                    border.Background = new SolidColorBrush(cellModel.CellStyle.Background);
                    Line rightLine = new Line()
                    {
                        X1 = 0,
                        X2 = 0,
                        Y1 = 0,
                        Y2 = height,
                        Width = cellModel.CellStyle.RightLineWidth,
                        Height = height,
                        Stroke = new SolidColorBrush(cellModel.CellStyle.RightLineColor),
                        StrokeThickness = cellModel.CellStyle.RightLineWidth
                    };
                    Canvas.SetLeft(rightLine, x+width);
                    Canvas.SetTop(rightLine, y);
                    mainCanvas.Children.Add(rightLine);
                    
                    Line bottomLine = new Line()
                    {
                        X1 = 0,
                        X2 = width,
                        Y1 = 0,
                        Y2 = 0,
                        Width = width,
                        Height = cellModel.CellStyle.BottomLineWidth,
                        Stroke = new SolidColorBrush(cellModel.CellStyle.BottomLineColor),
                        StrokeThickness = cellModel.CellStyle.BottomLineWidth
                    };
                    Canvas.SetLeft(bottomLine, x);
                    Canvas.SetTop(bottomLine, y + height);
                    mainCanvas.Children.Add(bottomLine);
                }
                if (cellModel.CellContent == null)
                    return;
                textBlock = new TextBlock()
                {
                    Text=cellModel.CellContent.DisplayString
                };
                Canvas.SetLeft(textBlock, x);
                Canvas.SetTop(textBlock, y);
                mainCanvas.Children.Add(textBlock);
            }
        }
    }
}
